import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.visitor.VoidVisitor;

import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class main {
    private static final String inputBase = "C:\\Users\\85124\\Desktop\\src2";
    private static final String outputBase = "C:\\Users\\85124\\Desktop\\out2";

    private static int count = 0;
    public static void main(String[] args) {
        Component
//        convertSingleFile();
//        return;

        List<String> files = getAllFile(inputBase);
        System.out.print(files.size());

        files.forEach(f-> {
            if (!isJavaFile(f)){
                return;
            }

            count ++;
            System.out.println( "解析第" + count + "个文件: " + f);

            CompilationUnit cu = null;
            try {
                cu = StaticJavaParser.parse(new FileInputStream(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            TextWriter textWriter = new TextWriter();
            VoidVisitor<TextWriter> visitor = new Visitor(true);
            visitor.visit(cu, textWriter);

            String out = f.replace(inputBase, outputBase).replace(".java", ".d.mi");
            writeFile(out, textWriter.toString());
        });
    }

    private static final String filePath = "C:\\Users\\85124\\Desktop\\main.java";
    private static void convertSingleFile() {
        CompilationUnit cu = null;
        try {
            cu = StaticJavaParser.parse(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        TextWriter textWriter = new TextWriter();
        VoidVisitor<TextWriter> visitor = new Visitor(true);
        visitor.visit(cu, textWriter);
        System.out.print(textWriter.toString());
    }

    private static boolean isJavaFile(String path) {
        File file = new File(path);
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        return suffix.equals("java");
    }

    private static void makeAll(String path) {
        File file = new File(path);
        if(!file.exists()){
            makeAll(file.getParent());
            file.mkdir();
        }
    }

    static void makeAll(int ... ame) {

    }

    private static String parent(String path) {
        File file = new File(path);
        return file.getParent();
    }

    private static void writeFile(String path, String content) {
        makeAll(parent(path));
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(path));
            out.write(content);
            out.close();
            System.out.println("写入文件: " + path + " 成功!");
        } catch (IOException e) {
            System.out.println("写入文件出错");
        }
    }

    public static List<String> getAllFile(String path) {
        List<String> result = new ArrayList<>();


        // int fileNum = 0, folderNum = 0;
        File file = new File(path);
        LinkedList<File> list = new LinkedList<>();

        if (file.exists()) {
            if (null == file.listFiles()) {
                return result;
            }
            list.addAll(Arrays.asList(file.listFiles()));
            while (!list.isEmpty()) {
                File[] files = list.removeFirst().listFiles();
                if (null == files) {
                    continue;
                }
                for (File f : files) {
                    if (f.isDirectory()) {
                        // System.out.println("文件夹:" + f.getAbsolutePath());
                        list.add(f);
                        // folderNum++;
                    } else {
                        // System.out.println("文件:" + f.getAbsolutePath());
                        result.add(f.getAbsolutePath());
                        // fileNum++;
                    }
                }
            }
        }
        return result;
    }
}
